package com.asiainfo.ncrm.base;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;






import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.asiainfo.ncrm.ant.AntApplication;
import com.asiainfo.ncrm.util.CustomProgressDialog;
import com.asiainfo.ncrm.util.HttpConstant;
import com.asiainfo.ncrm.util.X;
import com.asiainfo.ncrm.util.XUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import android.R.string;
import android.content.Context;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

public class BaseHttp {
	private static String TAG = "BaseHttp";

	//
	private HttpCallBack callBack;
	private Context context;
	
	private CustomProgressDialog customDialog;

	// public BaseHttp(int method, String url, JSONObject jsonRequest,
	// Listener<JSONObject> listener,
	// ErrorListener errorListener) {
	// }
	public BaseHttp(Context context) {
		this.context = context;
	}

	public void post(String url, Map<String, Object> params, final TypeToken typeToken) {
		JSONObject jsonObject;
		if (null == params) {
			jsonObject = new JSONObject();
		} else {
			jsonObject = new JSONObject(params);
		}
		startProgressDialog();
		StringRequest instance = new StringRequest(url, new  Listener<String>() {

			@Override
			public void onResponse(String response) {
				// TODO 自动生成的方法存根
				try{
				String json= response.toString();
				json=json.replace("\\", "");
				json=json.replace("\"{", "{");
				json=json.replace("}\"", "}");
				
					JSONObject jsonresult = new JSONObject(json);
					
					String resultCode =  jsonresult.getString("resultCode");
					
					if("0".equals(resultCode)){
						
					
				
					String resultjson = jsonresult.getString("result");
					X.d(TAG + "|success", resultjson);
					Result<?> result = new Gson().fromJson(json, typeToken.getType());
						if (HttpConstant.CODE_SUCCESS == result.getResultCode()) {
							callBack.onSuccess(result.getResultMsg(), result.getResult());
						} else {
							callBack.onFailure("code:" + result.getResultCode() + ",msg:" + result.getResultMsg(),
									result.getResult());
						}
					}else{
						callBack.onFailure(jsonresult.getString("resultMsg"), "");
					}
					stopProgressDialog();
				} catch (Exception e) {
					e.printStackTrace();
					stopProgressDialog();
//					Toast.makeText(context, "账户/密码错误", Toast.LENGTH_LONG).show();
					callBack.onFailure("code: -100" + ",msg:json解析失败", "");
				}
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				stopProgressDialog();
				// TODO 自动生成的方法存根
				X.d(TAG + "|error", error.toString());
				X.d(TAG + "|error", error.getCause());
				X.d(TAG + "|error", error.getStackTrace());
//				Toast.makeText(context, "登录失败，请重试", Toast.LENGTH_LONG).show();
//				XUtils.showText("服务端访问出错，" + error.getMessage());
				try {
					callBack2.callback(error);
				} catch (Exception e) {
				}
				
			}
		}) ;
		instance.setRetryPolicy(new DefaultRetryPolicy(50000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));   
		// RequestQueue queue = Volley.newRequestQueue(context);
		VolleyController.getInstance(context).addToRequestQueue(instance);
		// queue.add(inst
	}

	public void callBack(HttpCallBack callBack) {
		this.callBack = callBack;

	}
	
	BaseCallBack callBack2;
	public void FailCallBack(BaseCallBack callBack){
		this.callBack2=callBack;
	}
	
    private void startProgressDialog(){
		if (customDialog == null){
			customDialog = CustomProgressDialog.createDialog(context);
			customDialog.setMessage("请稍等.......");
		}
		
		customDialog.show();
	}
	
	private void stopProgressDialog(){
		if (customDialog != null){
			customDialog.dismiss();
			customDialog = null;
		}
	}

}
